package bTree.LeetCode;

import java.util.Stack;

public class 字符串解码 {
    public String decodeString(String s) {
        Stack<Character>stack=new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c!=']'){
                stack.add(c);
            }else {
                StringBuilder sb=new StringBuilder();
                while (stack.peek()!='['){
                    sb.append(stack.pop());
                }
                stack.pop(); //弹走‘【’
                StringBuilder num=new StringBuilder();
                while (!stack.isEmpty()&&stack.peek()>='0'&&stack.peek()<='9'){
                    num.append(stack.pop());
                }
                int number = Integer.valueOf(num.reverse().toString());
                String chars = sb.reverse().toString();
                for (int i1 = 0; i1 < number; i1++) {
                    for (int i2 = 0; i2 < chars.length(); i2++) {
                        stack.add(chars.charAt(i2));
                    }
                }

            }
        }
        StringBuilder res=new StringBuilder();
        for (Character c : stack) {
            res.append(c);
        }
        return res.toString();
    }
}